﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
using HengTong.Model.Db;
using HengTong.Model;
using HengTong.Model.Db.Other;


namespace HengTong.ViewModel.Activity.SysActivityParticipationVms
{
    public partial class
        SysActivityParticipationListVm : BasePagedListVM<SysActivityParticipationView, SysActivityParticipationSearcher>
    {
        protected override IEnumerable<IGridColumn<SysActivityParticipationView>> InitGridHeader()
        {
            return new List<GridColumn<SysActivityParticipationView>>
            {
                this.MakeGridHeader(x => x.ActivityName),
                this.MakeGridHeader(x => x.ParticipantType),
                this.MakeGridHeader(x => x.Name),
                this.MakeGridHeader(x => x.Phone),
                this.MakeGridHeader(x => x.Address),
                this.MakeGridHeader(x => x.Description),
                this.MakeGridHeader(x => x.UserName),
                this.MakeGridHeaderAction(width: 200)
            };
        }


        public override IOrderedQueryable<SysActivityParticipationView> GetSearchQuery()
        {
            if (Wtm.LoginUserInfo == null)
            {
                MSD.AddModelError(" ", "请先登录");
                return null;
            }

            //判断当前登陆人是否是游客角色999,或者不是超级管理员角色001
            bool isGuest = Wtm.LoginUserInfo.Roles.Any(e => e.RoleCode == "999");
            bool isNotAdmin = !Wtm.LoginUserInfo.Roles.Any(e => e.RoleCode == "001");
            if (Wtm.LoginUserInfo.Roles.Count == 0 || isGuest || isNotAdmin)
            {
                Searcher.UserId = Guid.Parse(Wtm.LoginUserInfo.UserId);
            }

            var query = DC.Set<SysActivityParticipation>()
                .CheckEqual(Searcher.ActivityId, x => x.ActivityId)
                .CheckEqual(Searcher.ParticipantType, x => x.ParticipantType)
                .CheckContain(Searcher.Name, x => x.Name)
                .CheckContain(Searcher.Phone, x => x.Phone)
                .CheckContain(Searcher.Address, x => x.Address)
                .CheckContain(Searcher.Description, x => x.Description)
                .CheckEqual(Searcher.UserId, x => x.UserId)
                .Select(x => new SysActivityParticipationView
                {
                    ID = x.ID,
                    ActivityName = x.Activity.ActivityName,
                    ParticipantType = x.ParticipantType,
                    Name = x.Name,
                    Phone = x.Phone,
                    Address = x.Address,
                    Description = x.Description,
                    UserName = x.User.Name,
                })
                .OrderBy(x => x.ID);
            return query;
        }
    }

    public class SysActivityParticipationView : SysActivityParticipation
    {
        [Display(Name = "活动名称")] public String ActivityName { get; set; }
        [Display(Name = "_Admin.Name")] public String UserName { get; set; }
    }
}